package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;

/*** 老师---备授课--授课报告-作业--阅读类详情 **/

@CrossOrigin
@RestController
public class LectureReportClassHomeReadDetailController extends YxyApiBaseController {

    @Autowired
    private HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    private HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/testreport/homework/detail/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String lectureId = reqModel.lectureId;
        String taskId = reqModel.taskId;
        String classId = reqModel.classId;
        String schoolId = getProfile(request, response).getSchoolId();
        Assert.notNull(StringUtils.defaultIfBlank(lectureId, null), "授课包ID为空");
        List<HomeworkTaskReport> hList = homeworkTaskReportRepository
                .findBySchoolIdAndClassIdAndLectureIdAndTaskId(schoolId, classId, lectureId, taskId);
        List<RespModel> respModels = new ArrayList<RespModel>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        RespModel model = null;
        for (HomeworkTaskReport homeworkTask : hList) {
            model = new RespModel();
            if (homeworkTask.getStatusOfFinished() == 1 || homeworkTask.getStatusOfFinished() == 2) {
                model.studentNick = homeworkTask.getStudentNick();
                model.finishedDate = sdf.format(homeworkTask.getFinishedDate());
                model.duration = homeworkTask.getDuration();
                List<HomeworkTaskReportQues> taskIdAndClassId = homeworkTaskReportQuesRepository
                        .findByTaskIdAndClassId(taskId, classId);
                for (HomeworkTaskReportQues homeworkTaskReportQues : taskIdAndClassId) {
                    if (homeworkTask.getStudentId().equals(homeworkTaskReportQues.getStudentId())) {
                        if (homeworkTaskReportQues.getNotifyPassed() != null) {
                            model.mark = homeworkTaskReportQues.getNotifyPassed();
                        }
                    }
                }
                respModels.add(model);
            }
        }
        return respModels;

    }

    protected static class ReqModel extends YxyApiReqModel {
        // 授课包id
        public String lectureId;
        // 任务ID
        public String taskId;
        // 班级ID
        public String classId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 授课包id
        public String lectureId;
        // 学生名称
        public String studentNick;
        // 提交时间
        public String finishedDate;
        // 阅读时长
        public Long duration;
        // 评分 -1 未批阅
        public Integer mark;
    }
}
